﻿/*
版权所有：版权所有(C) 2012，321Nets
文件编号：M01_NewsDao.cs
文件名称：NewsBLL.cs
系统编号：Z0003007
系统名称：通用办公系统
模块编号：M01
模块名称：信息发布
设计文档：IOA_M01信息发布XDE建模.mdx
完成日期：2012-5-5
作　　者：
内容摘要：表[Market]对应的业务逻辑 BLL
*/
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Configuration;


using Eis51.CampanyCMS.Model.NewsManager;

namespace Eis51.CampanyCMS.Access.NewsManager
{
    /// <summary>
    /// Market的BLL
    /// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
    /// </summary>
    public class MarketDao
    {
        #region 自动生成代码：取得相关实体

        //数据库连接字符串(web.config来配置)
        //<add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\\db1.mdb" />

        protected static string connectionString =
            string.Format(ConfigurationSettings.AppSettings["Eis_ConnectionString"],
                          System.Web.HttpContext.Current.Request.PhysicalApplicationPath);

        /// <summary>
        /// 方法名称: MarketDao
        /// 内容摘要: 构造函数进行初始化
        /// </summary>
        protected MarketDao()
        {
        }

        private static volatile MarketDao m_instance = null;
        /// <summary>
        /// 方法名称: GetInstance
        /// 内容摘要: 取得 MarketDao 对象
        /// </summary>
        /// <returns>返回一个已经存在的实体</returns>
        public static MarketDao GetInstance()
        {
            // 通用的必要代码 iBatisNet双校检机制,如果实例不存在
            if (m_instance == null)
            {
                lock (typeof(MarketDao))
                {

                    // 如果实例不存在
                    if (m_instance == null)
                    {

                        // 创建一个的实例
                        m_instance = new MarketDao();
                    }
                }
            }

            // 返回业务逻辑对象
            return m_instance;
        }

        /// <summary>
        /// 将DataReader的属性值转化为实体类的属性值，返回实体类
        /// </summary>
        /// <param name="dataReader">有效的DataReader对象</param>
        /// <returns>实体类对象</returns>
        //private static Market DataReaderToEntity(IDataReader dataReader)
        //{
        //	Market obj = null;			
        //    while (dataReader.Read())
        //    {
        //		obj = new Market();

        //
        //	}
        //	return obj;
        //}	
        /// <summary>
        /// 将DataReader的属性值转化为实体类的属性值，返回实体类
        /// </summary>
        /// <param name="dataReader">有效的DataReader对象</param>
        /// <returns>实体类对象</returns>
        private Market DataRowToEntity(DataRow dataReader)
        {
            Market obj = new Market();


            if (dataReader["Seqno"] != DBNull.Value) obj.Seqno = (int)dataReader["Seqno"];
            if (dataReader["Title"] != DBNull.Value) obj.Title = (string)dataReader["Title"];
            if (dataReader["Type"] != DBNull.Value) obj.Type = (int)dataReader["Type"];
            if (dataReader["Code"] != DBNull.Value) obj.Code = (string)dataReader["Code"];
            if (dataReader["Content"] != DBNull.Value) obj.Content = (string)dataReader["Content"];
            if (dataReader["ShowDescr"] != DBNull.Value) obj.Showdescr = (string)dataReader["ShowDescr"];
            if (dataReader["valid_Data"] != DBNull.Value) obj.ValidData = (DateTime)dataReader["valid_Data"];
            if (dataReader["Createby"] != DBNull.Value) obj.Createby = (string)dataReader["Createby"];
            if (dataReader["CreateDate"] != DBNull.Value) obj.Createdate = (DateTime)dataReader["CreateDate"];
            if (dataReader["LasteUpdateBy"] != DBNull.Value) obj.Lasteupdateby = (string)dataReader["LasteUpdateBy"];
            if (dataReader["LasteUpdateDate"] != DBNull.Value) obj.Lasteupdatedate = (DateTime)dataReader["LasteUpdateDate"];
            if (dataReader["scan_Times"] != DBNull.Value) obj.ScanTimes = (int)dataReader["scan_Times"];
            if (dataReader["Is_Valid"] != DBNull.Value) obj.IsValid = (int)dataReader["Is_Valid"];
            if (dataReader["Licd"] != DBNull.Value) obj.Licd = (int)dataReader["Licd"];
            if (dataReader["FileName"] != DBNull.Value) obj.Filename = (string)dataReader["FileName"];
            if (dataReader["IsFist"] != DBNull.Value) obj.Isfist = (int)dataReader["IsFist"];
            if (dataReader["IsHot"] != DBNull.Value) obj.Ishot = (int)dataReader["IsHot"];
            if (dataReader["show_idx"] != DBNull.Value) obj.ShowIdx = (int)dataReader["show_idx"];

            return obj;
        }

        /// <summary>
        /// 插入对象
        /// </summary>
        /// <param name="entity">
        /// 实体里的主键被赋值
        ///	</param>
        /// <returns>
        /// 返回当前插入的主键值
        ///</returns>
        public int Insert(Market entity)
        {
            int returnValue = -1;
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                try
                {

                    OleDbParameter p;
                    string sqlCommand = "Insert into Market([Title],[Type],[Code],[Content],[ShowDescr],[valid_Data],[Createby],[CreateDate],[LasteUpdateBy],[LasteUpdateDate],[scan_Times],[Is_Valid],[Licd],[FileName],[IsFist],[IsHot],[show_idx]) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
                    using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                    {
                        connection.Open();

                        p = new OleDbParameter("@Title", OleDbType.VarChar);
                        p.Value = entity.Title;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Type", OleDbType.Integer);
                        p.Value = entity.Type;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Code", OleDbType.VarChar);
                        p.Value = entity.Code;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Content", OleDbType.VarChar);
                        p.Value = entity.Content;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@ShowDescr", OleDbType.VarChar);
                        p.Value = entity.Showdescr;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@valid_Data", OleDbType.DBTimeStamp);
                        p.Value = DateTime.Parse(entity.ValidData.ToString("yyyy-MM-dd HH:mm:ss"));
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Createby", OleDbType.VarChar);
                        p.Value = entity.Createby;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@CreateDate", OleDbType.DBTimeStamp);
                        p.Value = DateTime.Parse(entity.Createdate.ToString("yyyy-MM-dd HH:mm:ss"));
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@LasteUpdateBy", OleDbType.VarChar);
                        p.Value = entity.Lasteupdateby;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@LasteUpdateDate", OleDbType.DBTimeStamp);
                        p.Value = DateTime.Parse(entity.Lasteupdatedate.ToString("yyyy-MM-dd HH:mm:ss"));
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@scan_Times", OleDbType.Integer);
                        p.Value = entity.ScanTimes;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Is_Valid", OleDbType.Integer);
                        p.Value = entity.IsValid;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Licd", OleDbType.Integer);
                        p.Value = entity.Licd;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@FileName", OleDbType.VarChar);
                        p.Value = entity.Filename;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@IsFist", OleDbType.Integer);
                        p.Value = entity.Isfist;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@IsHot", OleDbType.Integer);
                        p.Value = entity.Ishot;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@show_idx", OleDbType.Integer);
                        p.Value = entity.ShowIdx;
                        cmd.Parameters.Add(p);
                        cmd.ExecuteNonQuery();

                        sqlCommand = "SELECT TOP 1 Seqno FROM Market ORDER BY Seqno DESC";

                        cmd.CommandText = sqlCommand;
                        object o = cmd.ExecuteScalar();
                        entity.Seqno = Convert.ToInt32(o); ;
                        returnValue = entity.Seqno;
                    }
                }
                catch
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                }
            }
            return returnValue;
        }
        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(Market entity)
        {
            bool returnValue = false;
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {

                try
                {
                    OleDbParameter p;
                    string sqlCommand = "Update Market Set [Title] = ?,[Type] = ?,[Code] = ?,[Content] = ?,[ShowDescr] = ?,[valid_Data] = ?,[Createby] = ?,[CreateDate] = ?,[LasteUpdateBy] = ?,[LasteUpdateDate] = ?,[scan_Times] = ?,[Is_Valid] = ?,[Licd] = ?,[FileName] = ?,[IsFist] = ?,[IsHot] = ?,[show_idx] = ? where [Seqno] =?  ";
                    using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                    {
                        connection.Open();

                        p = new OleDbParameter("@Title", OleDbType.VarChar);
                        p.Value = entity.Title;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Type", OleDbType.Integer);
                        p.Value = entity.Type;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Code", OleDbType.VarChar);
                        p.Value = entity.Code;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Content", OleDbType.VarChar);
                        p.Value = entity.Content;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@ShowDescr", OleDbType.VarChar);
                        p.Value = entity.Showdescr;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@valid_Data", OleDbType.DBTimeStamp);
                        p.Value = DateTime.Parse(entity.ValidData.ToString("yyyy-MM-dd HH:mm:ss"));
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Createby", OleDbType.VarChar);
                        p.Value = entity.Createby;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@CreateDate", OleDbType.DBTimeStamp);
                        p.Value = DateTime.Parse(entity.Createdate.ToString("yyyy-MM-dd HH:mm:ss"));
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@LasteUpdateBy", OleDbType.VarChar);
                        p.Value = entity.Lasteupdateby;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@LasteUpdateDate", OleDbType.DBTimeStamp);
                        p.Value = DateTime.Parse(entity.Lasteupdatedate.ToString("yyyy-MM-dd HH:mm:ss"));
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@scan_Times", OleDbType.Integer);
                        p.Value = entity.ScanTimes;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Is_Valid", OleDbType.Integer);
                        p.Value = entity.IsValid;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Licd", OleDbType.Integer);
                        p.Value = entity.Licd;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@FileName", OleDbType.VarChar);
                        p.Value = entity.Filename;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@IsFist", OleDbType.Integer);
                        p.Value = entity.Isfist;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@IsHot", OleDbType.Integer);
                        p.Value = entity.Ishot;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@show_idx", OleDbType.Integer);
                        p.Value = entity.ShowIdx;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Seqno", OleDbType.Integer);
                        p.Value = entity.Seqno;
                        cmd.Parameters.Add(p);
                        cmd.ExecuteNonQuery();
                    }
                    returnValue = true;
                }
                catch
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                }
            }
            return returnValue;
        }

        /// <summary>
        /// 通过主键取得实体
        /// </summary>
        /// <param name="entity"> 主键 </param>
        /// <returns>实体对象</returns>
        public Market SelectByID(Market.Key entity)
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {

                try
                {
                    OleDbParameter p;
                    string sqlCommand = " select * From Market where [Seqno] = ?    ";
                    using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                    {
                        connection.Open();

                        p = new OleDbParameter("@Seqno", DbType.Int32);
                        p.Value = entity.Seqno;
                        cmd.Parameters.Add(p);
                        OleDbDataAdapter adp = new OleDbDataAdapter(sqlCommand, connection);
                        adp.SelectCommand = cmd;
                        DataTable dt = new DataTable();
                        adp.Fill(dt);
                        if (dt.Rows.Count > 0)
                        {
                            return DataRowToEntity(dt.Rows[0]);
                        }
                        else
                        {
                            return null;
                        }
                    }
                }
                catch
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                    return null;
                }
            }
        }

        public IList SelectAll()
        {
            ArrayList ar = new ArrayList();
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {

                string sqlCommand = "   select * From Market  ";
                using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                {
                    connection.Open();
                    OleDbDataAdapter adp = new OleDbDataAdapter(sqlCommand, connection);
                    adp.SelectCommand = cmd;
                    DataTable dt = new DataTable();
                    adp.Fill(dt);
                    foreach (DataRow dr in dt.Rows)
                    {
                        ar.Add(DataRowToEntity(dr));
                    }
                    return ar;
                }
            }

        }
        public bool DeleteByID(Market.Key entity)
        {
            try
            {
                using (OleDbConnection connection = new OleDbConnection(connectionString))
                {

                    OleDbParameter p;
                    string sqlCommand = " delete From Market where [Seqno] = ?    ";
                    using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                    {
                        connection.Open();

                        p = new OleDbParameter("@Seqno", DbType.Int32);
                        p.Value = entity.Seqno;
                        cmd.Parameters.Add(p);
                        cmd.ExecuteNonQuery();
                        return true;
                    }
                }
            }
            catch
            {
                return false;
            }
        }
        #endregion

    }
}
